package br.com.carser.mb;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.EJBTransactionRolledbackException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.com.carser.business.PermissionBusiness;
import br.com.carser.business.ProfileBusiness;
import br.com.carser.exception.DuplicateEntityException;
import br.com.carser.exception.ParentEntityException;
import br.com.carser.mb.infra.BaseMB;
import br.com.carser.model.Permission;
import br.com.carser.model.Profile;
import br.com.carser.util.MessageUtil;

/**
 * <B>Project: carser-web</B><BR>
 * 
 * JSF MB used for Role CRUD operations.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Sep 20, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Sep 20, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Sep 20, 2011
 * @version 1.0
 */
@ViewScoped
@ManagedBean
public class ProfileManager extends BaseMB {

	private static final long serialVersionUID = -8748664179837661018L;

	private static final String PROFILE_SAVE_SUCCESS = "faces.label.sucess.save.user.regitry";

	private static final String PROFILE_EXCLUDE_SUCCESS = "faces.label.sucess.exclude.user.regitry";
	
	private static final String QUERY_INSERT_DUPLICATE_ERROR = "faces.label.validation.duplicate.user.data";

	private static final String MESSAGE_ERROR_EXCLUDE_PROFILE_PARENT =  "faces.label.error.exclude.profile.regitry";
	
	private Profile profile = new Profile();
	
	private List<Profile> profiles;

	private List<Permission> permissions;
	
	private List<Permission> selectedPermissions;
	
	private boolean saved;
	
	private String name;
	
	@EJB
	private ProfileBusiness profileBusiness;
	
	@EJB
	private PermissionBusiness permissionBusiness;
	
	@PostConstruct
	public void init() {
		permissions = permissionBusiness.getAll();
	}
	
	public void reset() {
		saved = false;
		profile = new Profile();
		selectedPermissions = new ArrayList<Permission>();
	}
	
	/**
	 * Action to save a profile.
	 * 
	 * @return
	 */
	public void save() {
		try {
			profile.setPermissions(this.selectedPermissions);
			profileBusiness.saveOrUpdate(profile);
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, null, PROFILE_SAVE_SUCCESS);
			saved = true;
		} catch (DuplicateEntityException e) {
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_ERROR, null, QUERY_INSERT_DUPLICATE_ERROR);
		}
	}

	/**
	 * ActionListener to delete a profile.
	 * 
	 * @param e
	 * @return
	 * @throws ParentEntityException 
	 */
	public void delete() throws ParentEntityException {
		try {
			profileBusiness.delete(profile);
			profiles.remove(profile);
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, null, PROFILE_EXCLUDE_SUCCESS);
		} catch (EJBTransactionRolledbackException e) {
			MessageUtil.addFacesMessage(FacesMessage.SEVERITY_ERROR, null, MESSAGE_ERROR_EXCLUDE_PROFILE_PARENT);
		}
	}
	
	/**
	 * Edit a profile.
	 * 
	 * @param profile
	 */
	public void edit(Profile profile) {
		saved = false;
		this.profile = profileBusiness.getById(profile.getId());
//		this.selectedPermissions = profile.getPermissions();
	}

	/**
	 * Action to filter profiles.
	 */
	public void search() {
		this.profiles = profileBusiness.getByName(name);
	}

	/**
	 * @return the profile
	 */
	public Profile getProfile() {
		return profile;
	}

	/**
	 * @param profile the profile to set
	 */
	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	/**
	 * @return the profiles
	 */
	public List<Profile> getProfiles() {
		return profiles;
	}

	/**
	 * @return the permissions
	 */
	public List<Permission> getPermissions() {
		return permissions;
	}

	/**
	 * @param permissions the permissions to set
	 */
	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	/**
	 * @return the selectedPermissions
	 */
	public List<Permission> getSelectedPermissions() {
		return selectedPermissions;
	}

	/**
	 * @param selectedPermissions the selectedPermissions to set
	 */
	public void setSelectedPermissions(List<Permission> selectedPermissions) {
		this.selectedPermissions = selectedPermissions;
	}

	/**
	 * @return the saved
	 */
	public boolean isSaved() {
		return saved;
	}

	/**
	 * @param saved the saved to set
	 */
	public void setSaved(boolean saved) {
		this.saved = saved;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
}
